import com.mochimedia.ui.Tween;

/**
 * Class: TweenManager
 * Processes a queue of tweens.  Handles adding, removing and updating of tweened parameters.
 * @version 1.0
 */
class com.mochimedia.ui.TweenManager {

    private var _tweens:Array;
    private var _container:MovieClip;
    private var _tweener:MovieClip;
    
    private var _tweenID:Number = 0;
    
    private var _currentTime:Number = 0;
    
    // global timer so that all tweens sync
    public function get time ():Number { return _currentTime };
    
    public function get newTweenID ():Number {
        
        _tweenID++;
        return _tweenID;
        
    }
    
    //
    //
    //
    public function TweenManager (container:MovieClip, depth:Number) {
        
        init(container, depth);
        
    }
    
    //
    //
    //
    private function init (container:MovieClip, depth:Number):Void {
        
        _tweens = [];
        _currentTime = getTimer();
        _container = (container != null) ? container : _root;
        
        if (isNaN(depth) || depth == null) depth = _container.getNextHighestDepth();

        if (_container["_tweener"] == null) _container.createEmptyMovieClip("_tweener", depth);
        _tweener = _container["_tweener"];
        
        var self:Object = this;
        _tweener.onEnterFrame = function ():Void { self.update(); }
        
    }
    
    //
    //
    public function update ():Void {
    
        _currentTime = getTimer();
        
        for (var i:Number = 0; i < _tweens.length; i++) {
            
            Tween(_tweens[i]).ease();
            
        }
        
    }
    
    //
    //
    function align (clips:Array, property:String, value:Number):Void {
        
        for (var i:Number = 0; i < clips.length; i++) clips[i][property] = value;
        
    }
    
    //
    //
    //
    function createTweens (clips:Array, attribute:String, startVal:Object, endVal:Object, duration:Number, delay:Number, pixelSnap:Boolean, easeType:Number, easeStyle:Number):Void {

        if (isNaN(easeType) || easeType == null) easeType = Tween.EASE_IN;
        if (isNaN(easeStyle) || easeStyle == null) easeStyle = Tween.STYLE_LINEAR;
        if (isNaN(duration) || duration <= 0) duration = 1;

        for (var i:Number = 0; i < clips.length; i++) {
            if (typeof(clips[i]) == "movieclip") addTween(new Tween(this, clips[i], attribute, startVal, endVal, duration, delay, pixelSnap, easeType, easeStyle));
        }
        
    }
    
    
    //
    //
    //
    public function addTween (tween:Tween):Void {
        
        // clear conflicting tweens
        for (var i:Number = _tweens.length; i >= 0; i--) {
            if (Tween(_tweens[i]).clip == tween.clip && Tween(_tweens[i]).attribute == tween.attribute) { 
                _tweens.splice(i, 1);
                break;
            }
        }   
        
        _tweens.push(tween);
        
    }
    
    //
    //
    //
    public function removeTween (tween:Tween):Void {
        
        for (var i:Number = _tweens.length; i >= 0; i--) {
            if (_tweens[i].id == tween.id) { 
                _tweens.splice(i, 1);
                break;
            }
        }
        
    }
    
    //
    //
    //
    public function clearAllTweens ():Void {
    
        _tweens = [];
        
    }
    
    //
    //
    public function end ():Void {
        
        if (_tweener != null) {
            delete _tweener.onEnterFrame;
            _tweener.unloadMovie();
        }
        
    }
    
    
}